//
//  CXHTopOnNativeExpressRenderer.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/11.
//

#import "CXHTopOnNativeExpressRenderer.h"
#import <CXHAdSDK-Core/CXHAdSDKNativeExpressAd.h>
#import <CXHAdSDK-Adapter/CXHAdapterNativeExpressCallBack.h>
#import <CXHAdSDK-Adapter/CXHAdapterNativeAdViewDelegate.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@protocol ATNativeADView<NSObject>
@property (nonatomic, readonly) ATNativeADCache *nativeAd;
@end

@interface CXHTopOnNativeExpressRenderer ()

@property(nonatomic, strong) CXHTopOnNativeExpressEvent *expressEvent;
@property(nonatomic, strong) CXHTopOnNativeEvent *nativeEvent;
@property (nonatomic, assign) CGFloat expressAdHeight;
@property (nonatomic, strong) UIView *canvasView;
@property (nonatomic, assign) BOOL isAddKVOframe;

@end

@implementation CXHTopOnNativeExpressRenderer

- (void)bindCustomEvent{
    ATNativeADCache *cache = ((ATNativeADCache*)(((id<ATNativeADView>)self.ADView).nativeAd));
    BOOL isExpress = [cache.assets[kATNativeADAssetsIsExpressAdKey] boolValue];
    if (isExpress) {
        CXHTopOnNativeExpressEvent *customEvent = ((ATNativeADCache*)(((id<ATNativeADView>)self.ADView).nativeAd)).assets[kATAdAssetsCustomEventKey];
        customEvent.adView = self.ADView;
        self.ADView.customEvent = customEvent;
    } else {
        CXHTopOnNativeEvent *customEvent = ((ATNativeADCache*)(((id<ATNativeADView>)self.ADView).nativeAd)).assets[kATAdAssetsCustomEventKey];
        customEvent.adView = self.ADView;
        self.ADView.customEvent = customEvent;
    }
}

- (UIView *)getNetWorkMediaView{
    ATNativeADCache *cache = ((ATNativeADCache*)(((id<ATNativeADView>)self.ADView).nativeAd));
    BOOL isExpress = [cache.assets[kATNativeADAssetsIsExpressAdKey] boolValue];
    if (!isExpress) {
        UIView<CXHAdapterNativeAdViewDelegate> *nativeAdView = cache.assets[kATAdAssetsCustomObjectKey];
        return [nativeAdView cxh_mediaViewForWidth:nativeAdView.frame.size.width];
    }
    return nil;

}

-(void) renderOffer:(ATNativeADCache *)offer {
    [super renderOffer:offer];
    NSLog(@"========renderOffer");
    BOOL isExpress = [offer.assets[kATNativeADAssetsIsExpressAdKey] boolValue];
    if (isExpress) {
        CXHTopOnNativeExpressEvent *customEvent = ((ATNativeADCache*)(((id<ATNativeADView>)self.ADView).nativeAd)).assets[kATAdAssetsCustomEventKey];
        CXHAdSDKNativeExpressAd *nativeExpressAd = (CXHAdSDKNativeExpressAd *)offer.assets[@"kCXHNativeSelfRenderObj"];
        nativeExpressAd.delegate = customEvent;
        nativeExpressAd.controller = self.configuration.rootViewController;
        CGRect adFrame = self.ADView.frame;
        UIView<CXHAdapterNativeExpressAdViewDelegate> *nativeExpressAdView = offer.assets[kATAdAssetsCustomObjectKey];
        [nativeExpressAdView render];
        nativeExpressAdView.backgroundColor = self.ADView.backgroundColor;
        
        [nativeExpressAdView cxh_setRootViewController:self.configuration.rootViewController];
        
        CGFloat adWeight = [offer.assets[kATNativeADAssetsNativeExpressAdViewWidthKey] floatValue];
        CGFloat adHeight = [offer.assets[kATNativeADAssetsNativeExpressAdViewHeightKey] floatValue];
        
        self.ADView.frame = CGRectMake(adFrame.origin.x, adFrame.origin.y, adWeight, adHeight);
        [self.ADView addSubview:nativeExpressAdView];
    } else {
        CXHTopOnNativeEvent *customEvent = ((ATNativeADCache*)(((id<ATNativeADView>)self.ADView).nativeAd)).assets[kATAdAssetsCustomEventKey];
        CXHAdSDKNativeAd *nativeAd = (CXHAdSDKNativeAd *)offer.assets[@"kCXHNativeSelfRenderObj"];
        nativeAd.delegate = customEvent;
        nativeAd.controller = self.configuration.rootViewController;
        
        UIView<CXHAdapterNativeAdViewDelegate> *nativeAdView = offer.assets[kATAdAssetsCustomObjectKey];
        nativeAdView.backgroundColor = [UIColor clearColor];
        [self.ADView setNeedsLayout];
        [self.ADView layoutIfNeeded];
        nativeAdView.frame = self.ADView.bounds;
        NSMutableArray<UIView *> *views = [NSMutableArray array];
        [views addObjectsFromArray:[self.ADView clickableViews]];
        [views addObject:nativeAdView];
        [nativeAdView cxh_registViews:views];
        
        [nativeAdView cxh_setRootViewController:self.configuration.rootViewController];
        
        [self.ADView addSubview:nativeAdView];
    }
    
}

- (BOOL)isVideoContents{
    return NO;
}

- (ATNativeAdRenderType)getCurrentNativeAdRenderType {
    ATNativeADCache *cache = ((ATNativeADCache*)(((id<ATNativeADView>)self.ADView).nativeAd));
    BOOL isExpress = [cache.assets[kATNativeADAssetsIsExpressAdKey] boolValue];
    if (isExpress) {
        return ATNativeAdRenderExpress;
    } else {
        return ATNativeAdRenderSelfRender;
    }
}

- (void)dealloc {
    NSLog(@"------> %s", __FUNCTION__);
}

@end
